package io.github.wjehovah.yyn.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

import io.github.wjehovah.yyn.App;
import io.github.wjehovah.yyn.R;
import io.github.wjehovah.yyn.bean.CommentBean;

/**
 * Created by lcy on 2015/8/10.
 */
public class CommentAdaper extends BaseAdapter {//评论的lisview，的adapter；
    private Context mContext;
    private LayoutInflater mInflater;
    private List<CommentBean.DataInfo> data_latest;
    private List<CommentBean.DataInfo> data_hot;
    private BitmapUtils bitmapUtils;


    public CommentAdaper(Context mContext, List<CommentBean.DataInfo> data_hot, List<CommentBean.DataInfo> data_latest) {
        this.mContext = mContext;
        this.mInflater = LayoutInflater.from(mContext);
        this.data_latest = data_latest;
        this.data_hot = data_hot;
        bitmapUtils = new BitmapUtils(App.getContext(), "", 0.4f, 1024 * 1024 * 6);
    }

    @Override
    public int getCount() {
        if (data_hot.size() == 0) {
            return data_latest.size() + 1;
        } else {
            return data_hot.size() + data_latest.size() + 2;
        }
    }

    @Override
    public Object getItem(int position) {
        if (data_hot.size() == 0) {//没有最热评论；
            if (position == 0) {
                return 0;
            } else {
//                Log.e("LCy",data_latest.size());
                return data_latest.get(position - 1);///
            }
        } else {//有最热评论；
            if (position == 0 || position == data_hot.size() + 1) {
                return 1;
            } else if (position > 0 && position < data_hot.size() + 1) {
                return data_hot.get(position - 1);
            } else {
                return data_latest.get(position - data_hot.size() - 2);
            }
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (data_hot.size() == 0) {//没有最热评论；
            if (position == 0) {
                TextView tv = new TextView(mContext);
                tv.setTextSize(23);
                tv.setText("最新评论");
                return tv;
            }

            if (convertView == null || convertView instanceof TextView) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.comment_item, null);

                holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.comment = (TextView) convertView.findViewById(R.id.comment);

                holder.tv_ding_number = (TextView) convertView.findViewById(R.id.tv_ding_number);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
        } else {//有最热评论；
            if (position == 0 || position == data_hot.size() + 1) {
                if (position == 0) {
                    TextView tv = new TextView(mContext);
                    tv.setTextSize(23);
                    tv.setText("最热评论");
                    return tv;
                } else if (position == data_hot.size() + 1) {
                    TextView tv = new TextView(mContext);
                    tv.setTextSize(23);
                    tv.setText("最新评论");
                    return tv;
                }
            }
            if (convertView == null || convertView instanceof TextView) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.comment_item, null);

                holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.comment = (TextView) convertView.findViewById(R.id.comment);

                holder.tv_ding_number = (TextView) convertView.findViewById(R.id.tv_ding_number);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
        }

        CommentBean.DataInfo info = (CommentBean.DataInfo) getItem(position);

        BitmapUtils bitmapUtils = new BitmapUtils(mContext);
        bitmapUtils.display(holder.imageView, info.user.profile_image);
        holder.name.setText(info.user.username);
        holder.comment.setText(info.content);

        holder.tv_ding_number.setText(info.like_count);

        return convertView;
    }

    class ViewHolder {
        ImageView imageView;
        TextView name;
        TextView comment;
        TextView tv_ding_number;
    }
}
